<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model.trim="query.title" clearable placeholder="请输入标题" @keyup.enter.native="search" />
        <el-button type="primary" icon="el-icon-search" @click="search">
          {{ $t('table.search') }}
        </el-button> -->
      <el-button style="margin-left: 0;" type="primary" icon="el-icon-plus" @click="dialogVisible = true">
        新增
      </el-button>
    </div>
    <el-table :data="tableData" highlight-current-row :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }" border>
      <el-table-column prop="title" label="标题" sortable />
      <el-table-column prop="content" label="内容" sortable />
      <el-table-column prop="creator" label="发布人" sortable />
      <el-table-column prop="createTime" label="发布时间" sortable>
        <template slot-scope="{row}">
          <span>{{ row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="beginTime" label="展示开始时间" sortable>
        <template slot-scope="{row}">
          <span>{{ row.beginTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="endTime" label="展示结束时间" sortable>
        <template slot-scope="{row}">
          <span>{{ row.endTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleEdit(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row, $index)">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="ruleForm.id ? '编辑' : '新增'" :visible.sync="dialogVisible" @closed="handleClose" :close-on-click-modal="false">
      <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="标题" prop="title">
                <el-input v-model="ruleForm.title" placeholder="请输入标题"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="内容" prop="content">
                <el-input v-model="ruleForm.content"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="展示时间" prop="valueData">
            <el-date-picker style="width: 100%;" v-model="ruleForm.valueData" type="datetimerange" range-separator="-"
              start-placeholder="展示开始日期" end-placeholder="展示结束日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
    <el-pagination v-show="total > 0" style="margin-top:20px;" :current-page="query.pageIndex"
      :page-sizes="[5, 10, 20, 50]" :page-size="query.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>
<script>
import { getAllInfo, announceQuery, announceAdd,announceUpdate,announceDel } from '@/api/systemLog'
import { deepClone, arraryRemove } from '../../../utils'
export default {
  name: 'SystemLog',
  data() {
    return {
      query: {
        pageSize: 20,
        pageIndex: 1
      },
      tableData: [],
      total: 0,
      ruleForm: {
        valueData: []
      },
      dialogVisible: false,
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
        ],
        valueData: [
          { required: true, message: '请选择展示时间', trigger: 'change' }
        ]
      },
    }
  },
  mounted() {
    this.getInfor()
  },
  methods: {
    sure() {
      this.$refs['ruleForm'].validate(async valid => {
        if (valid) {
          const httpFn = this.ruleForm.id ? announceUpdate : announceAdd
          const { ruleForm: { valueData } } = this
          this.ruleForm.beginTime = valueData[0]
          this.ruleForm.endTime = valueData[1]
          await httpFn(this.ruleForm)
          this.getInfor()
          this.$message.success('操作成功')
          this.dialogVisible = false
        }
      })
    },
    handleEdit(row) {
      const { beginTime, endTime } = row
      this.ruleForm = deepClone(row)
      this.ruleForm.valueData = [beginTime, endTime]
      this.dialogVisible = true
    },
    handleDelete({id}) {
      this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await announceDel(id)
          this.$message.success('删除成功')
        })
    },
    handleClose() {
      this.$refs['ruleForm'].resetFields()
    },
    handleSizeChange(size) {
      this.query.pageSize = size
    },
    handleCurrentChange(currentPage) {
      this.query.pageIndex = currentPage
    },
    search() {
      this.getInfor()
    },
    checkSpecialKey(str) {
      var specialKey = "[`~!#$^&*()=|{}':;'\\[\\]<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘'"
      for (var i = 0; i < str.length; i++) {
        if (specialKey.indexOf(str.substr(i, 1)) != -1) {
          return false
        }
      }
      return true
    },

    getInfor() {
      announceQuery(this.query).then(res => {
        this.tableData = res.items
        this.total = res.totalPages
      })
    }
  }
}
</script>